home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Miscellaneous / Randy Thelen / ThreadedSort / Misc Stuff / Synchronization.cp < prev    next >
Encoding:
Text File  |  1994-06-26  |  1.7 KB  |  96 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Synchronization.cp
  3.  
  4.     Contains:    Some classes useful for synchronizing multiple threads or tasks.
  5.                 Simple semaphores and NuKernel-style EventGroups are implemented.
  6.                 
  7.     Written by: Dave Falkenburg
  8.  
  9.     Copyright:    © 1993 by Dave Falkenburg, all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     
  13.  */
  14.  
  15. #include "Synchronization.h"
  16.  
  17. /*
  18.     TO DO:        Get this working at interrupt level
  19.                 Add sanity checks?
  20.  */
  21.  
  22. TSimpleSemaphore::TSimpleSemaphore(void)
  23.     {
  24.     fCounter = 1;
  25.     fOwner = kNoThreadID;
  26.     fResultCode = noErr;
  27.     
  28.     //    *** Don’t forget to initialize the waiting list ***
  29.     }
  30.  
  31. TSimpleSemaphore::~TSimpleSemaphore(void)
  32.     {
  33.     //    Wake up everybody waiting for this semaphore
  34.  
  35.     ThreadBeginCritical();
  36.         fSleepCount = 0;
  37.         fOwner = kNoThreadID;    //    this indicates an error!
  38.     ThreadEndCritical();
  39.     
  40.     //    *** Don’t forget to wakeup the whole waiting list ***
  41.     }
  42.  
  43. OSErr
  44. TSimpleSemaphore::Up(void)
  45.     {
  46.     ThreadID    thisThread;
  47.     OSErr        resultCode;
  48.  
  49.     GetCurrentThread(&thisThread);
  50.     ThreadBeginCritical();
  51.  
  52.         fCounter++;
  53.         if (fCounter == 0)
  54.             {
  55.             }
  56.  
  57.     ThreadEndCritical();
  58.     }
  59.  
  60. OSErr
  61. TSimpleSemaphore::Down(void)
  62.     {
  63.     ThreadID    thisThread;
  64.     OSErr        resultCode;
  65.     
  66.     GetCurrentThread(&thisThread);
  67.     ThreadBeginCritical();
  68.  
  69.         fCounter--;
  70.         if (fCounter == 0)
  71.             {
  72.             //    We got the semaphore (yea!)
  73.             
  74.             fOwner = thisThread;
  75.             }
  76.         else if (fCounter < 0)
  77.             {
  78.             //    Someone else has the semaphore, wait for it…
  79.             //    Put us on the waiting list and go to sleep.
  80.     
  81.             SetThreadStateEndCritical(thisThread,kStoppedThreadState,fOwner);
  82.             ThreadBeginCritical();
  83.     
  84.             //    We’ve been awakened! Remember: this doesn’t mean we got
  85.             //    the lock, somebody may have destroyed the semaphore!
  86.     
  87.             if (fOwner != thisThread)
  88.                 resultCode = threadProtocolErr;
  89.             }
  90.  
  91.     ThreadEndCritical();
  92.  
  93.     return resultCode;
  94.     }
  95.  
  96.